package com.wjm.blog.common.util.bigdataexcel;

import java.util.ArrayList;
import java.util.List;


/**
 * @author BUG WRITTER
 * @date 2022/10/9 15:08
 */
public class ListGroupUtil {

    /**
     * 将集合按指定数量分组
     *
     * @param list     数据集合
     * @param quantity 分组数量
     * @return 分组结果
     */
    public static <T> List<List<T>> groupListByQuantity(List<T> list, int quantity) {
        if (list == null || list.size() == 0) {
            return null;
        }

        if (quantity <= 0) {
            throw new IllegalArgumentException("Wrong quantity.");
        }

        List<List<T>> wrapList = new ArrayList<List<T>>();
        int count = 0;
        while (count < list.size()) {
            wrapList.add(new ArrayList<T>(list.subList(count, Math.min((count + quantity), list.size()))));
            count += quantity;
        }

        return wrapList;
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i <= 2011; i++) {
            list.add(i);
        }

        List<List<Integer>> resultList = ListGroupUtil.groupListByQuantity(list, 1000);
        for (List<Integer> l : resultList) {
            System.out.println(l);
        }
    }

}
